﻿<%@ WebHandler Language="C#" Class="DownLoadHandler" %>

using System;
using System.Web;
using System.Text;

public class DownLoadHandler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) 
    {
        string filePath = context.Request["url"];
        string fileName = context.Request["name"];
        
        //以字符流的形式下载文件
        System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open);
        byte[] bytes = new byte[(int)fs.Length];
        fs.Read(bytes, 0, bytes.Length);
        fs.Close();
        context.Response.ContentType = "application/octet-stream";
        //通知浏览器下载文件而不是打开
        //context.Response.Charset = "GB2312";
        //context.Response.ContentEncoding = "GB2312";
        context.Response.AddHeader("Content-Disposition", "attachment; filename="+ HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(fileName)));
        context.Response.BinaryWrite(bytes);
        context.Response.Flush();
        context.Response.End();
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}